package com.yantong.service.impl;

import com.yantong.constant.MemberType;
import com.yantong.mapper.CarMapper;
import com.yantong.mapper.MemberMapper;
import com.yantong.pojo.Car;
import com.yantong.pojo.ListMemberParams;
import com.yantong.pojo.Member;
import com.yantong.service.CommanderListService;
import com.yantong.service.CommissionService;
import com.yantong.utils.PageUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *  操作Member服务
  * @Description:
  * Copyright: Copyright (c) 2018 
  * @author xiangyi
  * @version V1.0.0
  * @since 3.0
  * @date 2018年9月3日 下午6:55:16
 */
@Service
public class CommanderListServiceImpl implements CommanderListService {
    
    private static final Logger logger = LoggerFactory.getLogger(CommanderListServiceImpl.class);
    
    @Autowired
    private MemberMapper memberMapper;
    
    @Autowired
    private CarMapper carMapper;
    
    @Autowired
    private CommissionService commissionService;
    

    @Override
    public PageUtils<Member> listCommanders(ListMemberParams listMemberParams, Member member, int offset, int limit){
        logger.info("list listCommanders, listMemberParams:{}, member:{}, offset:{}, limit:{}", listMemberParams, member, offset, limit);
        PageUtils<Member> pageUtils = new PageUtils<>();
        HashMap<String, Object> searchMap = new HashMap<String, Object>();
        searchMap.put("isteam", MemberType.COMMANDER.getCode());
        if(StringUtils.isNotEmpty(listMemberParams.getStartDate())){
            searchMap.put("startDate", listMemberParams.getStartDate());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getEndDate())){
            searchMap.put("endDate", listMemberParams.getEndDate());
        }
        if(StringUtils.isNotEmpty(member.getNickname())){
            searchMap.put("nickname", member.getNickname());
        }
        if(StringUtils.isNotEmpty(member.getMobile())){
            searchMap.put("mobile", member.getMobile());
        }
        if(member.getId() != null){
            searchMap.put("id", member.getId());
        }
        if(listMemberParams.getCentreId() != null) {
            searchMap.put("centreId", listMemberParams.getCentreId());
        }
//        if(listMemberParams.getLiansuoId() != null) {
//            searchMap.put("teamid", listMemberParams.getLiansuoId());
//        }
        if(listMemberParams.getLiansuoId() != null) {
            searchMap.put("id", listMemberParams.getLiansuoId());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getKeyword())){
            searchMap.put("keyword", listMemberParams.getKeyword());
        }
        searchMap.put("ORDERLIST", "t.createtime desc");
        int total = memberMapper.getCountByMemberWhere(searchMap);
        if (total >0) {
            logger.info("listCommanders, total size:{}", total);
            pageUtils.setTotal(total);
            List<Member> memberList = memberMapper.getListByMemberWhere(searchMap, new RowBounds(offset, limit));
            if(CollectionUtils.isNotEmpty(memberList)){
                memberList.forEach((e) -> {
                     e.setCommissionTotal(commissionService.commissionCount(e.getId(),1));
                    Car car = carMapper.getByTmid(e.getId());
                    if(car != null){
                        e.setCarName(car.getUsername());
                    }
                     /*CarXq carXq = carXqService.getCarXqByXqName(e.gettXiaoqu());
                     if(carXq != null){
                         Car car = carMapper.selectByPrimaryKey(carXq.getCarId());
                         if(car != null){
                             e.setCarName(car.getUsername());
                         }
                     }*/
                });
            }
            pageUtils.setRows(memberList);
        }else {
            logger.info("listCommanders not found");
            pageUtils.setRows(new ArrayList<>());
        }
        return pageUtils;
    }
    
    @Override
    public List<Member> listCommanders(ListMemberParams listMemberParams, Member member){
        logger.info("Export listCommanders, listMemberParams:{}, member:{}", listMemberParams, member);
        HashMap<String, Object> searchMap = new HashMap<String, Object>();
        searchMap.put("isteam", MemberType.COMMANDER.getCode());
        if(StringUtils.isNotEmpty(listMemberParams.getFollow())){
            searchMap.put("follow", Integer.parseInt(listMemberParams.getFollow()));
        }
        if(StringUtils.isNotEmpty(listMemberParams.getStartDate())){
            searchMap.put("startDate", listMemberParams.getStartDate());
        }
        if(StringUtils.isNotEmpty(listMemberParams.getEndDate())){
            searchMap.put("endDate", listMemberParams.getEndDate());
        }
        if(StringUtils.isNotEmpty(member.getNickname())){
            searchMap.put("nickname", member.getNickname());
        }
        if(StringUtils.isNotEmpty(member.getMobile())){
            searchMap.put("mobile", member.getMobile());
        }
        if(member.getId() != null){
            searchMap.put("id", member.getId());
        }
        if(listMemberParams.getCentreId() != null) {
            searchMap.put("centreId", listMemberParams.getCentreId());
        }
        searchMap.put("ORDERLIST", "t.createtime desc");
        int total = memberMapper.getCountByMemberWhere(searchMap);
        if (total >0) {
            logger.info("Export listCommanders, total size:{}", total);
            List<Member> memberList = memberMapper.getListByMemberWhere(searchMap);
            return memberList;
        }else {
            logger.info("Export listCommanders not found");
           return null;
        }
    }
    
}
